const express = require('express')
const router= express.Router()

module.exports = router

const userRouter = require('./user')
const recordRouter = require('./record')


// 处理请求体数据
router.use(
    express.urlencoded({extended:false}),
    express.json(),
)

// 允许所有接口
router.use(function(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,PATCH,DELETE,OPTIONS");
    next()
})

router.use('/user',userRouter)
router.use('/record',recordRouter)


// 处理跨域
// const whilelist = [
//     'http://localhost:8080',
//     'http://localhost:5173'
// ]
// router.use(function(req,res,next){
    // Origin:'http://localhost:8080'
    // const origin = req.get('Origin')
    // console.log(origin)
    // // 判断origin是否在白名单中
    // if(whilelist.includes(origin)){
    //     // 数组方法:includes/indexOf
    //     res.set({
    //         // 简单跨域：设置`Access-Control-Allow-Origin`响应头
    //         'Access-Control-Allow-Origin':origin
    //     })

    //     // 处理复杂跨域中的预检请求
    //     if(req.method=="OPTIONS"){
    //         res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    //         res.header("Access-Control-Allow-Methods","PUT,POST,GET,PATCH,DELETE,OPTIONS");
    //         res.sendStatus(200); //让options请求快速返回
    //     }else{
    //         next()
    //     }
    // }else{
    //     next()
    // }
// })


// 利用http-proxy-middleware实现代理
// const {createProxyMiddleware} = require('http-proxy-middleware')
// // 当请求地址中包含/api/proxy时，进入该代理
// router.use(
//     '/offer', 
//     createProxyMiddleware({ 
//         target: 'http://localhost:5137', 
//         changeOrigin: true ,
//         pathRewrite: {
//             '^/api/proxy':'http://localhost:5137'
//         }
//     })
// );

